/*
wKeyBindings V0.2

Copyright (C) 2008  Jan Hoersch
*/

// MOOTOOLS REQUIRED http://mootools.net
// 	At Least: V1.11

Array.implement({

	/**
	 * compare - Returns true for equal Arrays
	 *
	 * @example
	 *	[1,4,5,2,5].compare([4,5,1,5,2]) == true
	 *	[1,4,5,2,5].compare([1,4,5,2,5},true) == true
	 *	[1,4,5,2,5].compare([4,1,2,5,5},true) == false
	 *	[1,3,2].compare([2,4,1]) == false
	 *
	 * @return	{Boolean}
	 * @param	{Object} Array
	 *		{Boolean} Position Sensitiv
	 */
	compare: function(array,position) {
		if(this.length != array.length) return false;

		if(position) {
			for(i = 0; i < this.length; i++) {
				if(this[i] != array[i]) return false;
			}
		} else {	
			var contain = false;
			for(i = 0; i < this.length; i++) {
				for(y = 0; y < array.length; y++) {
					if(this[i] == array[y]) {
						array = array.unset(y);	
						contain = true;
						break;
					}
				}
				if(!contain) {
					return false;
				} else {
					contain = false;
				}
			}
		}
		return true;
	},


	/**
	 * unset - Unsetting a element in the array by there Index
	 *
	 * @example
	 *	[1,5,3].unset(1) == [1,3]
	 *
	 * @return	{Object} Array
	 * @param	{Object} Array
	 */
	unset: function(key) {
		var buffer = new Array();
		this.each(function(item,index){
			if(key != index) {
				buffer[buffer.length] = item;
			}
		});
		return buffer;
	},
});

wKeybindings = new Class({
	options: {
		pressedKey: [],
		actions: [],
		keys: {
			'tab' : 9,
			'enter' : 13,
			'shift' : 16,
			'lstrg' : 17,
			'lalt' : 18,
			'capslock' : 20,

			'space' : 32,

			'left' : 37,
			'up' : 38,
			'right' : 39,
			'down' : 40,

			'0' : 48,
			'1' : 49,
			'2' : 50,
			'3' : 51,
			'4' : 52,
			'5' : 53,
			'6' : 54,
			'7' : 55,
			'8' : 56,
			'9' : 57,

			'a' : 65,
			'b' : 66,
			'c' : 67,
			'd' : 68,
			'e' : 69,
			'f' : 70,
			'g' : 71,
			'h' : 72,
			'i' : 73,
			'j' : 74,
			'k' : 75,
			'l' : 76,
			'm' : 77,
			'n' : 78,
			'o' : 79,
			'p' : 80,
			'q' : 81,
			'r' : 82,
			's' : 83,
			't' : 84,
			'u' : 85,
			'v' : 86,
			'w' : 87,
			'x' : 88,
			'y' : 89,
			'z' : 90,

			'lApfel' : 91,
			'rApfel' : 93,
		} 
	},

	initialize: function(options) {
		this.setOptions(options);

		window.addEvent('keydown', function(event) {
			event = new Event(event);
			if(!this.options.pressedKey.contains(event.code)) {
				this.options.pressedKey.push(event.code);
			}

			$('debug').innerHTML = this.options.pressedKey.join(',');
		
			return this.check();
		}.bind(this));

		window.addEvent('keyup', function(event) {
			event = new Event(event);
			this.options.pressedKey.erase(event.code);

			$('debug').innerHTML = this.options.pressedKey.join(',');
		}.bind(this));
	},

	check: function() {
		this.options.actions.each(function(item, index) {
			if(item[0].compare(this.options.pressedKey)) {
				eval(item[1]);

				this.options.pressedKey.empty();
				return true;
			}
		},this);
	},

	addAction: function(pKey,script) {
		pKey = pKey.map(function(item,index) {
			if($type(item) == 'string') {
				eval('ret = this.options.keys.'+item+';');

				return ret;
			}
			else if($type(item) == 'number') {
				return item;
			}
		},this);

		this.options.actions.push([pKey,script]);
	}
});
wKeybindings.implement(new Events, new Options);
